package com.aier.admin.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin.entity.SysCity;
import com.aier.common.common.CityUtil;
import com.aier.common.service.ISysCityService;

/**
 * 区域管理
 * 
 * @author Administrator
 * 
 */
@Scope("prototype")
@Component("adminCityAction")
public class AdminCityAction extends AdminBaseAction {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private Map<String, String> province;
    @Resource(name = "sysCityService")
    private ISysCityService sysCityService;

    private List<SysCity> lsc;

    /**
     * 选择城市id
     */
    private Integer cid;
    private String name;
    private boolean choosed;
    private int s;

    /**
     * 进入区域管理页面
     */
    @Override
    public String execute() {
        List<SysCity> lsc = sysCityService.searchAllProvince(null);
        province = new HashMap<String, String>();
        if (null == lsc || lsc.size() <= 0) {
            return SUCCESS;
        }
        for (SysCity sc : lsc) {
            province.put(sc.getCid()+"_"+sc.getCstate(), sc.getName());
        }
        return SUCCESS;
    }

    /**
     * 加载城市数据
     * 
     * @return
     */
    public String loadCity() {
        if (null == cid || cid <= 0) {
            s = 0;
            return SUCCESS;
        }
        lsc = sysCityService.searchCitySub(cid, null);
        s = 1;
        return SUCCESS;
    }

    /**
     * 修改城市数据
     * 
     * @return
     */
    public String updateCity() {        
        if (null == cid || cid <= 0) {
            s = 0;
            return SUCCESS;
        }
        short cstate;
        if(choosed){
            cstate = ISysCityService.ON;
        }else{
            cstate = ISysCityService.OFF;
        }
        s = sysCityService.updateCityName(cid, name, cstate);

        return SUCCESS;
    }
    
    public String delCity(){
        
        sysCityService.removeCity(cid);
        s=1;
        return SUCCESS;
    }
    
    /**
     * 增加城市数据
     * 
     * @return
     */
    public String addCity() {
        if (null == cid || cid <= 0) {
            s = 0;
            return SUCCESS;
        }
        short cstate;
        if(choosed){
            cstate = ISysCityService.ON;
        }else{
            cstate = ISysCityService.OFF;
        }
        SysCity sc = sysCityService.getCityByCid(cid);
        s = sysCityService.insertCity(sc, name, cstate);

        return SUCCESS;
    }
    
    /**
     * 增加子列城市数据
     * 
     * @return
     */
    public String addSubCity() {
        if (null == cid || cid <= 0) {
            s = 0;
            return SUCCESS;
        }
        short cstate;
        if(choosed){
            cstate = ISysCityService.ON;
        }else{
            cstate = ISysCityService.OFF;
        }
        SysCity sc = sysCityService.getCityByCid(cid);
        s = sysCityService.insertSubCity(sc, name, cstate);
        return SUCCESS;
    }
    
    public String refresh(){
        CityUtil.initLoad(sysCityService);
        s=1;
        return SUCCESS;
    }
    
    public Map<String, String> getProvince() {
        return province;
    }

    public int getS() {
        return s;
    }

    public void setSysCityService(ISysCityService sysCityService) {
        this.sysCityService = sysCityService;
    }

    public List<SysCity> getLsc() {
        return lsc;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setChoosed(boolean choosed) {
        this.choosed = choosed;
    }



    
}
